import Vue from 'vue'
import VueRouter from 'vue-router'
import Register from '../views/Register.vue'
import Login from '../views/Login.vue'
import Index from '../views/Index.vue'
import Header from '../layout/Header'
import Footer from '../layout/Footer'
import MusicList from '../views/MusicList'
import MusicSheet from '../views/MusicSheet'
import Singer from '../views/Singer'
import Menu from '../layout/Menu'
import MusicSheetDetail from '../views/MusicSheetDetail'
import WebSocketTest from '../views/WebSocketTest'
import SingerDetail from '../views/SingerDetail'
import PlayMusic from '../views/PlayMusic'
import UserDetail from '../views/UserDetail'
import MusicListV2 from '../views/MusicListV2'
import MusicSheetV2 from '../views/MusicSheetV2'
import SingerAlbumDetail from '../views/SingerAlbumDetail'
import Forget from '../views/Forget'
import UpdatePassword from '../views/UpdatePassword'
import PersonalCenter from '../views/PersonalCenter'
import PlayMusicComment from '../views/PlayMusicComment'
import SocialIndex from '../views/SocialIndex'
import PlayMusicHistory from '../views/PlayMusicHistory'
import Search from '../views/Search'
import Chat from '../layout/Chat'

const originalReplace = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalReplace.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/register',
    name: 'register',
    component: Register,
    meta:{title:'注册'}
  },
  {
    path: '/login',
    name: 'login',
    components: {
      header: Header,
      default: Login,
      footer: Footer
    },
    meta:{title:'登录'}
  },
  {
    path: '/index',
    name: 'index',
    component: Index,
    meta:{title:'酷猫主页', keepAlive: false}
  },
  {
    path: '/header',
    name: 'header',
    component: Header
  },
  {
    path: '/menu',
    name: 'menu',
    component: Menu
  },
  {
    path: '/musicList',
    name: 'musicList',
    component: MusicList
  },
  {
    path: '/musicSheet',
    name: 'musicSheet',
    component: MusicSheet
  },
  {
    path: '/singer',
    name: 'singer',
    component: Singer
  },
  {
    path: '/musicSheetDetail',
    name: 'musicSheetDetail',
    component: MusicSheetDetail
  },
  {
    path: '/webSocketTest',
    name: 'webSocketTest',
    component: WebSocketTest
  },
  {
    path: '/playMusic',
    name: 'playMusic',
    component: PlayMusic
  },
  {
    path: '/singerDetail',
    name: 'singerDetail',
    component: SingerDetail
  },
  {
    path: '/userDetail',
    name: 'userDetail',
    component: UserDetail
  },
  {
    path: '/musicListV2',
    name: 'musicListV2',
    component: MusicListV2
  },
  {
    path: '/musicSheetV2',
    name: 'musicSheetV2',
    component: MusicSheetV2
  },
  {
    path: '/singerAlbumDetail',
    name: 'singerAlbumDetail',
    component: SingerAlbumDetail
  },
  {
    path: '/forget',
    name: 'forget',
    component: Forget
  },
  {
    path: '/updatePassword',
    name: 'updatePassword',
    component: UpdatePassword
  },
  {
    path: '/personalCenter',
    name: 'personalCenter',
    component: PersonalCenter
  },
  {
    path: '/playMusicComment',
    name: 'playMusicComment',
    component: PlayMusicComment
  },
  {
    path: '/socialIndex',
    name: 'socialIndex',
    component: SocialIndex
  },
  {
    path: '/playMusicHistory',
    name: 'playMusicHistory',
    component: PlayMusicHistory
  },
  {
    path: '/search',
    name: 'search',
    component: Search
  },
  {
    path: '/chat',
    name: 'chat',
    component: Chat
  }

]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  if (to.path === '/register') return next()
  if (to.path === '/index') return next()
  if (to.path === '/musicList') return next()
  if (to.path === '/musicListV2') return next()
  if (to.path === '/musicSheet') return next()
  if (to.path === '/musicSheetV2') return next()
  if (to.path === '/singer') return next()
  if (to.path === '/singerDetail') return next()
  if (to.path === '/musicSheetDetail') return next()
  if (to.path === '/webSocketTest') return next()
  if (to.path === '/singerAlbumDetail') return next()
  if (to.path === '/playMusic') return next()
  if (to.path === '/forget') return next()
  if (to.path === '/updatePassword') return next()
  if (to.path === '/playMusicComment') return next()
  if (to.path === '/socialIndex') return next()
  if (to.path === '/search') return next()
  if (to.path === '/chat') return next()

  // 如果用户访问的网站不是/login 获取token
  const localToken = window.localStorage.getItem('token')
  const sessionToken = window.sessionStorage.getItem('token')
  if (localToken || sessionToken) return next()
  // 如果程序执行到这一行 说明token没有数据，跳转到主页面
  next('/index')
})

export default router
